﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using Newtonsoft.Json;
using SH.STD.Model;
using Cmn;
using SH.STD.DataProvider;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for ProjectHandler
    /// </summary>
    public class ProjectHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "application/json;charset=gb2312";

            switch (context.Request.Params["action"])
            {
                case "loadData":
                    context.Response.Write(ConvertToJsonString(context.Request.Params["id"], context.Request.Params["category"]));
                    break;
                case "addData":
                    context.Response.Write(AddData(context.Request.Params["jsonDataString"]));
                    break;
                case "removeData":
                    context.Response.Write(RemoveData(context.Request.Params["jsonDataString"]));
                    break;
                case "updateData":
                    context.Response.Write(UpdateData(context.Request.Params["jsonDataString"]));
                    break;
            }
        }

        private string AddData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
INSERT INTO Project ([ProjectID] ,[OldID] ,[Name] ,[Category] ,[StartDate] ,[EndDate] ,[DepID] ,[ParentTypeID] ,[ChildTypeID] ,
                    [TypeDesc] ,[OutFund] ,[TotalFund] ,[PrjContent] ,[CheckIndex],[StatusID] ,[EvaluateDate] ,
                    [AwardID] ,[AwardDesc] ,[Apply] ,[LastModifiedBy])
            VALUES (@ProjectID ,@OldID ,@Name ,@Category ,@StartDate ,@EndDate ,@DepID ,@ParentTypeID ,@ChildTypeID ,
                    @TypeDesc ,@OutFund ,@TotalFund ,@PrjContent ,@CheckIndex,@StatusID ,@EvaluateDate ,
                    @AwardID ,@AwardDesc ,@Apply ,@LastModifiedBy);SELECT @@identity";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ProjectID",RepsonseInfo.Decode(item["ProjectID"].ToString())),
                        new SqlParameter("OldID",RepsonseInfo.Decode(item["OldID"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(item["Name"].ToString())),
                        new SqlParameter("Category",item["Category"]),
                        new SqlParameter("StartDate",item["StartDate"]),
                        new SqlParameter("EndDate",item["EndDate"]),
                        new SqlParameter("DepID",RepsonseInfo.Decode(item["DepID"].ToString())),
                        new SqlParameter("ParentTypeID",RepsonseInfo.Decode(item["ParentTypeID"].ToString())),
                        new SqlParameter("ChildTypeID",RepsonseInfo.Decode(item["ChildTypeID"].ToString())),
                        new SqlParameter("TypeDesc",RepsonseInfo.Decode(item["TypeDesc"].ToString())),
                        new SqlParameter("OutFund",item["OutFund"]),
                        new SqlParameter("TotalFund",item["TotalFund"]),
                        new SqlParameter("PrjContent",RepsonseInfo.Decode(item["PrjContent"].ToString())),
                        new SqlParameter("CheckIndex",RepsonseInfo.Decode(item["CheckIndex"].ToString())),
                        new SqlParameter("StatusID",RepsonseInfo.Decode(item["StatusID"].ToString())),
                        new SqlParameter("EvaluateDate",item["EvaluateDate"]),
                        new SqlParameter("AwardID",RepsonseInfo.Decode(item["AwardID"].ToString())),
                        new SqlParameter("AwardDesc",RepsonseInfo.Decode(item["AwardDesc"].ToString())),
                        new SqlParameter("Apply",RepsonseInfo.Decode(item["Apply"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(item["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID });
        }

        private string UpdateData(string jsonDataString)
        {
            JavaScriptObject project = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
Update Project Set 
ProjectID=@ProjectID,
OldID=@OldID,
Name=@Name,
Category=@Category,
StartDate=@StartDate,
EndDate=@EndDate,
DepID=@DepID,
ParentTypeID=@ParentTypeID,
ChildTypeID=@ChildTypeID,
TypeDesc=@TypeDesc,
OutFund=@OutFund,
TotalFund=@TotalFund,
PrjContent=@PrjContent,
CheckIndex=@CheckIndex,
StatusID=@StatusID,
EvaluateDate=@EvaluateDate,
AwardID=@AwardID,
AwardDesc=@AwardDesc,
Apply=@Apply,
LastModifiedBy=@LastModifiedBy
WHERE ID=@ID";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(project["ID"].ToString())),
                        new SqlParameter("ProjectID",RepsonseInfo.Decode(project["ProjectID"].ToString())),
                        new SqlParameter("OldID",RepsonseInfo.Decode(project["OldID"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(project["Name"].ToString())),
                        new SqlParameter("Category",RepsonseInfo.Decode(project["Category"].ToString())),
                        new SqlParameter("StartDate",RepsonseInfo.Decode(project["StartDate"].ToString())),
                        new SqlParameter("EndDate",RepsonseInfo.Decode(project["EndDate"].ToString())),
                        new SqlParameter("DepID",RepsonseInfo.Decode(project["DepID"].ToString())),
                        new SqlParameter("ParentTypeID",RepsonseInfo.Decode(project["ParentTypeID"].ToString())),
                        new SqlParameter("ChildTypeID",RepsonseInfo.Decode(project["ChildTypeID"].ToString())),
                        new SqlParameter("TypeDesc",RepsonseInfo.Decode(project["TypeDesc"].ToString())),
                        new SqlParameter("OutFund",RepsonseInfo.Decode(project["OutFund"].ToString())),
                        new SqlParameter("TotalFund",RepsonseInfo.Decode(project["TotalFund"].ToString())),
                        new SqlParameter("PrjContent",RepsonseInfo.Decode(project["PrjContent"].ToString())),
                        new SqlParameter("CheckIndex",RepsonseInfo.Decode(project["CheckIndex"].ToString())),
                        new SqlParameter("StatusID",RepsonseInfo.Decode(project["StatusID"].ToString())),
                        new SqlParameter("EvaluateDate",RepsonseInfo.Decode(project["EvaluateDate"].ToString())),
                        new SqlParameter("AwardID",RepsonseInfo.Decode(project["AwardID"].ToString())),
                        new SqlParameter("AwardDesc",RepsonseInfo.Decode(project["AwardDesc"].ToString())),
                        new SqlParameter("Apply",RepsonseInfo.Decode(project["Apply"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(project["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = RepsonseInfo.Decode(project["ID"].ToString()) });
        }

        private string ConvertToJsonString(string id, string category)
        {
            DataTable dt = null;
            var sql = String.Empty;
            SqlParameter[] cmdParams = null;
            if (String.IsNullOrEmpty(id))
            {
                sql = @"
SELECT  [ID] ,[ProjectID] ,[OldID] ,[Name] ,[Category] ,[StartDate] ,[EndDate] ,[DepID] ,[ParentTypeID] ,[ChildTypeID] ,[TypeDesc] ,[OutFund] ,
[TotalFund] ,[PrjContent] ,[CheckIndex] ,[Achievement] ,[StatusID] ,[EvaluateDate] ,[AwardID] ,[AwardDesc] ,[Apply] ,[LastModifiedBy],[LastModified],Memo
FROM [Project] 
WHERE status=1";
                dt = Cmn.DB.getDataTable(sql, cmdParams);
            }
            else
            {
                sql = @"
SELECT  [ID] ,[ProjectID] ,[OldID] ,[Name] ,[Category] ,[StartDate] ,[EndDate] ,[DepID] ,[ParentTypeID] ,[ChildTypeID] ,[TypeDesc] ,[OutFund] ,
[TotalFund] ,[PrjContent] ,[CheckIndex] ,[Achievement] ,[StatusID] ,[EvaluateDate] ,[AwardID] ,[AwardDesc] ,[Apply] ,[LastModifiedBy],[LastModified],Memo
FROM [Project] 
WHERE ID=@ID and status=1";

                cmdParams = new SqlParameter[]
                {
                    new SqlParameter("ID",id.ToInt()),
                };
                dt = Cmn.DB.getDataTable(sql, cmdParams);
            }
            return DataAccess.ConvertToJSON(dt, "Projects");
        }

        private string RemoveData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(item["ID"].ToString())),
                    };
            var sql = "update Project set status=0 where ID=@ID";
            Cmn.DB.execSql(sql, cmdParas);

            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = item["ID"].ToString() });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}